﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PUCP.SI2.SGIRP.BE;
using PUCP.SI2.SGIRP.BC;

namespace PUCP.SI2.SGIRP.GUI
{
    public partial class NuevoLocalForm : Form
    {
        public string TextPlano="";
        public string TextFoto="";
        public LocalMenuForm w;
        public int tipo = 1;
        LocalBE lc = null;

        public NuevoLocalForm() { 
        
        }
        public NuevoLocalForm(int tipo ,LocalMenuForm w ,LocalBE lc)
        {
            InitializeComponent();
            this.w = w;
            this.tipo = tipo;
            this.lc = lc;

            List<CentroComercialBE> listaC = LocalBC.getAllCC();

            for (int i = 0; i < listaC.Count; i++)
            {

                cbCC.Items.Add(listaC[i].Nombre);
            }

            List<TipoLocalBE> lista = LocalBC.getAllTipos();

            for (int i = 0; i < lista.Count; i++)
            {

                cbTipo.Items.Add(lista[i].Descripcion);

            }


            if (tipo == 2 || tipo == 3 ) {

                cbTipo.Text = lc.Tipo.Descripcion;
                CentroComercialBE cc = LocalBC.searchCentroComercial(lc.CodigoCC);
                cbCC.Text = cc.Nombre;
                cbNivel.Text = ""+lc.Nivel;


                at.Text = ""+lc.AreaTerraza;
                av.Text =""+ lc.AreaVitrina ;
                al.Text =""+lc.AreaLocal ;
                ato.Text =""+lc.AreaTotal;
                codigo.Text = lc.CodigoLocal;

                if (lc.Estado == 1)
                {
                    radio.Checked = true;

                }
                else
                {
                    radio2.Checked = true;
                }


                if (tipo == 3) {

                    radio.Enabled = false;
                    radio2.Enabled = false;
                    ato.Enabled = false;
                    av.Enabled = false;
                    at.Enabled = false;
                    al.Enabled = false;
                    codigo.Enabled = false;
                    cbCC.Enabled = false;
                    cbNivel.Enabled = false;
                    cbTipo.Enabled = false;

                    button4.Enabled = false;
                }

            
            
            }

          
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = this.openFileDialog1.ShowDialog();
            // if a file is selected
            if (result == DialogResult.OK)
            {
                // Set the selected file URL to the textbox
            TextFoto = this.openFileDialog1.FileName;
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult result = this.openFileDialog2.ShowDialog();
            // if a file is selected
            if (result == DialogResult.OK)
            {
                // Set the selected file URL to the textbox
                 TextPlano = this.openFileDialog2.FileName;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            LocalBE l = new LocalBE();
           

                l.AreaTerraza = float.Parse(at.Text);
                l.AreaVitrina = float.Parse(av.Text);
                l.AreaLocal = float.Parse(al.Text);
                l.AreaTotal = l.AreaLocal + l.AreaVitrina + l.AreaVitrina;
                l.CodigoLocal = codigo.Text;
                l.Plano = TextPlano;
                l.Foto = TextFoto;
                l.Estado = radio.Checked ? 1 : 0;
                l.EstadoLocal = radio.Checked ? "ACTIVO" : "INACTIVO";
                l.CodigoCC = LocalBC.Listacentro[cbCC.SelectedIndex].Codigo;
                l.Tipo = LocalBC.Listatipo[cbTipo.SelectedIndex];
                l.Nivel = cbNivel.SelectedIndex + 1;
           if (tipo == 1)
              {
                LocalBC.addLocal(l);
                MessageBox.Show("Cambios Realizados Satisfactoriamente");
                this.Close();
            }
           if (tipo == 2)
           {
               l.Codigo = lc.Codigo;
               LocalBC.update(l);
               MessageBox.Show("Cambios Realizados Satisfactoriamente");
               this.Close();

           }

          

            //l.Centrocomercial = GestorLocal.CcL[cbCC.SelectedIndex];
            //l.Nivel = cbNivel.SelectedIndex + 1;
            //l.Tipolocal = GestorLocal.tp;

            //GestorLocal.addLocal(l);
            //w.refreshTable(GestorLocal.LocalL);
            //clearWindow();
         

        }

        public void clearWindow(){
            at.Text = "";
            av.Text = "";
            al.Text = "";
            codigo.Text = "";
            
            radio.Checked = false;
            radio2.Checked = false;
            TextPlano = "";
            TextFoto = "";
            cbCC.Text = "";
            cbNivel.Text = "";
            cbTipo.Text = "";

        
        }

        private void al_TextChanged(object sender, EventArgs e)
        {
            try
            {

                ato.Text = "" + (float.Parse(at.Text) + float.Parse(av.Text) + float.Parse(al.Text));
            }
            catch (Exception ex)
            {

            }
        }

        private void av_TextChanged(object sender, EventArgs e)
        {
            try
            {

                ato.Text = "" + (float.Parse(at.Text) + float.Parse(av.Text) + float.Parse(al.Text));
            }
            catch (Exception ex)
            {

            }
        }

        private void at_TextChanged(object sender, EventArgs e)
        {
            try
            {

                ato.Text = "" + (float.Parse(at.Text) + float.Parse(av.Text) + float.Parse(al.Text));
            }
            catch (Exception ex)
            {

            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void NuevoLocalForm_Load(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
